<script setup lang='ts'>
import { getConsultPayOrderUrl } from '@/apis/consult';
import { showLoadingToast, showToast } from 'vant';
import { ref } from 'vue';

const payMethods = ref<0 | 1>()
const emit = defineEmits<{
  'update:show': [void: boolean]
}>()
const props = defineProps<{
  show: boolean
  orderId: string
  onClose?: () => void
  actualPayment: number
  payCallback: string
}>()
const OnPay = async () => {
  if (payMethods.value === undefined) return showToast('请选择支付方式')
  showLoadingToast({ message: '跳转支付', duration: 0 })
  const res = await getConsultPayOrderUrl({ paymentMethod: payMethods.value, orderId: props.orderId, payCallback: 'http://localhost:8080' + props.payCallback })
  location.href = res.data.payUrl
}
</script>
<template>
  <van-action-sheet :close-on-click-overlay="false" @close="onClose" :closeable="true" :close-on-popstate="false"
    :show="show" @update:show="emit('update:show', $event)" title="选择支付方式">
    <!-- 支付类型 -->
    <div class="pay-type">
      <p class="amount">￥{{ actualPayment.toFixed(2) }}</p>
      <van-cell-group>
        <van-cell title="微信支付" @click="payMethods = 0">
          <template #icon><cp-icon name="consult-wechat" /></template>
          <template #extra><van-checkbox :checked="payMethods === 0" /></template>
        </van-cell>
        <van-cell title="支付宝支付" @click="payMethods = 1">
          <template #icon><cp-icon name="consult-alipay" /></template>
          <template #extra><van-checkbox :checked="payMethods === 1" /></template>
        </van-cell>
      </van-cell-group>
      <div class="btn">
        <van-button type="primary" round block @click="OnPay">立即支付</van-button>
      </div>
    </div>
  </van-action-sheet>

</template>
<style lang="scss" scoped>
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }

  .btn {
    padding: 15px;
  }

  .van-cell {
    align-items: center;

    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }

    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>